import type Form from '@arco-design/web-vue/es/form/form'
import type { FieldRule } from '@arco-design/web-vue/es/form/interface'
import type { Ref } from 'vue'


export type ArcoForm = InstanceType<typeof Form>

export type ArcoFormRules<T = Record<string, any>> = Partial<Record<keyof T, Array<FieldRule>>>


export class ArcoFormTool {

    /**
     * 表单验证
     * @return Promise 完成为验证通过, 拒绝为验证失败
     */
    public static async validate(formRef: Ref<ArcoForm | null>): Promise<void> {
        const formInstance = formRef.value
        if (!formInstance) {
            return
        }

        const errors = await formInstance.validate()
        return !errors
                ? Promise.resolve()
                : Promise.reject(errors)
    }

}
